﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using UBCon.Core.Commands;

namespace UBCon.Ribbon
{
    /// <summary>
    /// Represents a selectable item inside a UBCon.Ribbon.TabControl. 
    /// </summary>
    public class TabItem:HeaderedContentControl 
    {
        //------------------------------------------------------
        //
        //  Constructors
        //
        //------------------------------------------------------
        #region Constructors

        /// <summary>
        /// Initializes a new instance of UBCon.Ribbon.TabControlItem class.
        /// </summary>
        public TabItem() 
        {
            RegisterCommands();
        }

        /// <summary>
        /// Default static constructor.
        /// </summary>
        static TabItem() 
        {
            Type ownerType = typeof(TabItem);
            DefaultStyleKeyProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(ownerType));
        }
        #endregion

        //------------------------------------------------------
        //
        //  Properties
        //
        //------------------------------------------------------
        #region Properties

        /// <summary>
        /// Gets or sets a boolean value that indicates whether this item is last one or not.
        /// </summary>
        internal bool IsLastItem
        {
            get { return (bool)GetValue(IsLastItemProperty); }
            set { SetValue(IsLastItemProperty, value); }
        }

        // <summary>
        /// Identifies UBCon.Ribbon.TabItem.IsLastItem dependeny property.
        /// </summary>
        internal static readonly DependencyProperty IsLastItemProperty =
            DependencyProperty.Register("IsLastItem", typeof(bool), typeof(TabItem), 
            new PropertyMetadata(false));

        

        /// <summary>
        /// Gets or sets a boolean value that indicates whether UBCon.Ribbon.TabItem is selected.
        /// </summary>
        public bool IsSelected
        {
            get { return (bool )GetValue(IsSelectedProperty); }
            set { SetValue(IsSelectedProperty, value); }
        }

        /// <summary>
        /// Identifies UBCon.Ribbon.TabItem.IsSelected dependeny property.
        /// </summary>
        public static readonly DependencyProperty IsSelectedProperty = TabControlSelector.IsSelectedProperty.AddOwner(typeof(TabItem));

        /// <summary>
        /// Gets or sets the command that closes this item.
        /// </summary>
        internal Command CloseCommand
        {
            get { return (Command)GetValue(CloseCommandProperty); }
            set { SetValue(CloseCommandProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CloseCommand.  This enables animation, styling, binding, etc...
        internal static readonly DependencyProperty CloseCommandProperty =
            DependencyProperty.Register("CloseCommand", typeof(Command), typeof(TabItem), new PropertyMetadata((Command)null));

        
        #endregion

        //------------------------------------------------------
        //
        //  Depenency Property Changed Callbacks
        //
        //------------------------------------------------------
        #region Depenency Property Changed Callbacks
        #endregion

        //------------------------------------------------------
        //
        //  Overrides
        //
        //------------------------------------------------------
        #region Overrides

        /// <summary>
        /// Invoked when an unhandled System.Windows.UIELement.MouseLeftButtonDown routed event is raised on this element.
        /// </summary>
        protected override void OnMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            
            if (ParentSelector != null) 
            {
                int index = ParentSelector.ItemContainerGenerator.IndexFromContainer(this);
                if (ParentSelector.SelectedIndex != index) 
                {
                    if (ParentSelector.SelectedIndex >= 0) 
                    {
                        DependencyObject prevousItem = ParentSelector.ItemContainerGenerator.ContainerFromIndex(ParentSelector.SelectedIndex);
                        if (prevousItem != null) 
                        {
                            prevousItem.ClearValue(TabItem.IsSelectedProperty);
                        }
                    }

                    ParentSelector.SetValue(TabControlSelector.SelectedIndexProperty, index);
                }
            }
        }

        #endregion

        //------------------------------------------------------
        //
        //  Virual members
        //
        //------------------------------------------------------
        #region Virual members

        #endregion

        //------------------------------------------------------
        //
        //  Public Methods
        //
        //------------------------------------------------------
        #region Public Methods
        #endregion

        //------------------------------------------------------
        //
        //  Internal Methods
        //
        //------------------------------------------------------
        #region Internal Methods
        #endregion

        //------------------------------------------------------
        //
        //  Private Methods
        //
        //------------------------------------------------------
        #region Private Methods

        /// <summary>
        /// Registers all commands.
        /// </summary>
        private void RegisterCommands() 
        {
            CloseCommand = new Command(OnCloseCommandExecuted);
        }

        /// <summary>
        /// Invoked when CloseCommand is executed.
        /// </summary>
        private void OnCloseCommandExecuted(object parameter) 
        {
            if (ParentSelector != null) 
            {
                ParentSelector.Items.Remove(this);
            }
            FrameworkElement item = Content as FrameworkElement;
            if (item != null && item.DataContext != null && item.DataContext is IDisposable) 
            {
                (item.DataContext as IDisposable).Dispose();
            }

            IDisposable _iDisposable = Content as IDisposable;
            if (_iDisposable != null) 
            {
                _iDisposable.Dispose();
            }
            this.ClearValue(ContentProperty);
        }

        #endregion

        //------------------------------------------------------
        //
        //  Event Handlers
        //
        //------------------------------------------------------
        #region Event Handlers
        #endregion

        //------------------------------------------------------
        //
        //  Fields
        //
        //------------------------------------------------------
        #region Fields

        internal TabControlSelector ParentSelector;
        
        #endregion

        //------------------------------------------------------
        //
        //  Events
        //
        //------------------------------------------------------
        #region Events
        #endregion

        //------------------------------------------------------
        //
        //  Commands
        //
        //------------------------------------------------------
        #region Commands
        #endregion

    }
}
